package cn.qh.com.controller;

import cn.qh.com.annotationverification.group.AddGroup;
import cn.qh.com.entity.Category;
import cn.qh.com.exception.NotFoundException;
import cn.qh.com.service.CategoryService;
import cn.qh.com.vo.CommonResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.qh.com.entity.PmsAttrGroup;
import cn.qh.com.dto.PmsAttrGroupDTO;
import cn.qh.com.service.PmsAttrGroupService;
import net.sf.jsqlparser.expression.NotExpression;
import org.springframework.jdbc.object.SqlQuery;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.BeanUtils;

/**
 * 控制层
 * 控制层
 **/
@RestController
@RequestMapping("pmsAttrGroup")
@Api
public class PmsAttrGroupController {
    /**
     * 服务对象
     */
    @Resource
    private PmsAttrGroupService pmsAttrGroupService;

    @Resource
    private CategoryService categoryService;

    /**
     * 分页查询所有数据
     *
     * @param current 分页对象
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询所有数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = CommonResponse.class)
    @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int")
    @CrossOrigin
    public Page<PmsAttrGroup> selectAll(@RequestParam(defaultValue = "1", required = false) int current,
                                        @RequestParam(defaultValue = "", name = "attrGroupName", required = false) String attrGroupName,
                                        @RequestParam(defaultValue = "0", name = "catelogId", required = false) int catelogId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!attrGroupName.trim().equals("")) {
            queryWrapper.like("attr_group_name", attrGroupName);
        }

        if (catelogId != 0) {
            queryWrapper.eq("catelog_id", catelogId);
        }

        Page page = new Page();
        page.setCurrent(current);
        page.setSize(3);
        this.pmsAttrGroupService.page(page, queryWrapper);
        return page;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @CrossOrigin
    @ApiOperation(value = "通过主键查询单条数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = CommonResponse.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    public PmsAttrGroup selectOne(@PathVariable(name = "id") Serializable id) {
        //有点小bug
        PmsAttrGroup pmsAttrGroup = this.pmsAttrGroupService.getById(id);
        long catelogId = pmsAttrGroup.getCatelogId();
        while (true) {
            Category byId1 = categoryService.getById(catelogId);
            if (byId1 == null) {
                break;
            }
            pmsAttrGroup.getCatLogPath().add(catelogId);
            catelogId = byId1.getParentcid();
        }
        return pmsAttrGroup;
    }

    /**
     * 新增数据
     *
     * @param pmsAttrGroupDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = CommonResponse.class)
    @CrossOrigin
    public boolean insert(@RequestBody @Validated(AddGroup.class) PmsAttrGroupDTO pmsAttrGroupDTO) {
        Long catelogId = pmsAttrGroupDTO.getCatelogId();
        Category byId = categoryService.getById(catelogId);
        if (byId == null) {
            throw new NotFoundException(30001);
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("attr_group_name", pmsAttrGroupDTO.getAttrGroupName());
        queryWrapper.eq("catelog_id", pmsAttrGroupDTO.getCatelogId());
        int count = pmsAttrGroupService.count(queryWrapper);
        if (count > 0) {
            throw new NotFoundException(30002);
        }


        PmsAttrGroup pmsAttrGroup = new PmsAttrGroup();
        BeanUtils.copyProperties(pmsAttrGroupDTO, pmsAttrGroup);
        return this.pmsAttrGroupService.save(pmsAttrGroup);
    }

    /**
     * 修改数据
     *
     * @param pmsAttrGroupDTO 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改信息", produces = "application/json;charset=utf-8",
            httpMethod = "PUT", response = CommonResponse.class)
    @CrossOrigin
    public boolean update(@RequestBody PmsAttrGroupDTO pmsAttrGroupDTO) {
        Long catelogId = pmsAttrGroupDTO.getCatelogId();
        Category byId = categoryService.getById(catelogId);
        if (byId == null) {
            throw new NotFoundException(30001);
        }

        Long attrGroupId = pmsAttrGroupDTO.getAttrGroupId();
        PmsAttrGroup pmsAttrGroup = pmsAttrGroupService.getById(attrGroupId);

        BeanUtils.copyProperties(pmsAttrGroupDTO, pmsAttrGroup);
        return this.pmsAttrGroupService.updateById(pmsAttrGroup);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除信息", produces = "application/json;charset=utf-8",
            httpMethod = "DELETE", response = CommonResponse.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    @CrossOrigin
    public boolean delete(@PathVariable Serializable id) {
        return this.pmsAttrGroupService.removeById(id);
    }
}
